package com.example.binding_recyclerview.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.binding_recyclerview.adapter.MyRecycleAdapter;
import com.example.binding_recyclerview.model.Student;
import com.example.binding_recyclerview.myInter.OnItemClick;

/**
 * 这里是继承了ViewModel对象，可以防止屏幕旋转的时候vm对象丢失数据
 * 如果想要为我们自己的vm对象在初始化的时候设置一个Application对象，就继承自AndroidViewModel
 * 如果不需要Application 直接继承自ViewModel就可以了
 *
 * 继承自viewmodel或者AndroidViewModel 的时候，需要避免在外部通过new的方式创建RecycleViewMoel
 * 而是通过new ViewModelProvider(this).get(RecycleViewMoel.class); 这样的方式拿到viewMode对象
 *
 * ObservableField 不支持在后台线程中更新数据，不提供线程安全的更新数据
 *
 *
 * 所以好像使用MutableLiveData 会更好，可以在后台更新数据
 * MutableLiveData 是 LiveData 的子类，提供了一个线程安全的观察者模式实现，可以在后台线程中更新数据，并确保这些更新在主线程上通知观察者。
 * MutableLiveData 的 setValue() 和 postValue() 方法可以用来更新数据，其中 setValue() 必须在主线程中调用，而 postValue() 可以在任何线程中调用。
 * MutableLiveData 适合用作 ViewModel 中的数据存储，因为它可以被外部组件（如 Activity 或 Fragment）观察，并且可以在数据变化时自动更新 UI。
 *
 *
 */
public  class  RecycleViewMoel extends AndroidViewModel{

    private static ObservableField<Student> currentStu = new ObservableField<>();

    private ObservableArrayList<Student> students= new ObservableArrayList<>();

    //碎片的视图对象 后面又说不建议在vm中防止view对象，有内存溢出的风险
    //private static View fragmentView = null;

    public RecycleViewMoel(@NonNull Application application) {
        super(application);

        Student student1 = new Student();
        student1.setId("1");
        student1.setName("one");
        Student student2 = new Student();
        student2.setId("2");
        student2.setName("two");
        Student student3 = new Student();
        student3.setId("three");
        student3.setName("three");

        students.add(student1);
        students.add(student2);
        students.add(student3);
    }


//    public RecycleViewMoel(Application application) {
//        super(application);
//
//        Student student1 = new Student();
//        student1.setId("1");
//        student1.setName("one");
//        Student student2 = new Student();
//        student2.setId("2");
//        student2.setName("two");
//        Student student3 = new Student();
//        student3.setId("three");
//        student3.setName("three");
//
//        students.add(student1);
//        students.add(student2);
//        students.add(student3);
//    }
//    public RecycleViewMoel(View fragmentView) {
//
//        this.fragmentView = fragmentView;
//
//        Student student1 = new Student();
//        student1.setId("1");
//        student1.setName("one");
//        Student student2 = new Student();
//        student2.setId("2");
//        student2.setName("two");
//        Student student3 = new Student();
//        student3.setId("three");
//        student3.setName("three");
//
//        students.add(student1);
//        students.add(student2);
//        students.add(student3);
//    }

    public ObservableField<Student> getCurrentStu() {
        return currentStu;
    }

    public void setCurrentStu(ObservableField<Student> currentStu) {
        this.currentStu = currentStu;
    }

    public ObservableArrayList<Student> getStudents() {
        return students;
    }

    public void setStudents(ObservableArrayList<Student> students) {
        this.students = students;
    }

    //点击事件
    public void clickadd(View view){
        //拿到碎片上的数据
        Student student = new Student();
        student.setId(currentStu.get().getId());
        student.setName(currentStu.get().getName());
        students.add(student);
    }

    public void clickRemove(View view){
        students.remove(currentStu.get());
        currentStu.set(null);
    }

    //为recycle绑定数据和适配器
    @BindingAdapter("android:entries")
    public static void  bindDataForRecycle(RecyclerView recyclerView , ObservableArrayList<Student> students){
        //判断适配器是否是空
        MyRecycleAdapter myRecycleAdapter = (MyRecycleAdapter) recyclerView.getAdapter();
        if( myRecycleAdapter == null ){
            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
            myRecycleAdapter = new MyRecycleAdapter(students);

            //通过这个接口，我可以把想要处理的数据都传递到view中进行处理，这样就不用给Adapter传递其他的参数了
            myRecycleAdapter.setOnItemClick(new OnItemClick<Student>() {
                @Override
                public void click(Student student) {
                    currentStu.set(student);
                }
            });
            recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
            recyclerView.addItemDecoration(dividerItemDecoration);//添加分割线);
            recyclerView.setHasFixedSize(true); //不用重新计算布局大小，提升效率
            recyclerView.setAdapter(myRecycleAdapter);
        }else {
            myRecycleAdapter.notifyDataSetChanged();
        }
    }
}
